package com.genshang.restaurant.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genshang.restaurant.entity.Consumptionlist;
import com.genshang.restaurant.entity.Couponlist;
import com.genshang.restaurant.mapper.CouponlistMapper;
import com.genshang.restaurant.result.ResultBody;
import com.genshang.restaurant.service.ICouponlistService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Alex
 * @since 2020-10-19
 */
@Service
public class CouponlistServiceImpl extends ServiceImpl<CouponlistMapper, Couponlist> implements ICouponlistService {

    @Override
    public ResultBody exchangeCoupon(String id, String operatorId) {
        QueryWrapper<Couponlist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        Couponlist couponlist = this.getOne(queryWrapper);
        if (couponlist == null) {
            return ResultBody.error("没有该券");
        }
        if (!"有效".equals(couponlist.getState())) {
            return ResultBody.error("该券失效");
        }
        if (!"未使用".equals(couponlist.getCouponstate())) {
            return ResultBody.error("该券已经使用");
        }
        couponlist.setCouponstate("已使用");
        couponlist.setModifytime(System.currentTimeMillis());
        couponlist.setUsedtime(System.currentTimeMillis());
        couponlist.setOperaterid(operatorId);
        return ResultBody.success(this.updateById(couponlist));
    }

    @Override
    public ResultBody getCouponList(Long currentPage, Long pageSize, String couponId, String couponType, String couponName, String personNumber, String phone) {
        Page page = new Page(currentPage, pageSize);
        QueryWrapper<Couponlist> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(couponId)) {
            queryWrapper.eq("ID", couponId);
        }
        if (StringUtils.isNotBlank(couponType)) {
            queryWrapper.eq("TYPE", couponType);
        }
        if (StringUtils.isNotBlank(couponName)) {
            queryWrapper.eq("MODALITY", couponName);
        }
        if (StringUtils.isNotBlank(personNumber)) {
            queryWrapper.eq("NUMBER", personNumber);
        }
        if (StringUtils.isNotBlank(phone)) {
            queryWrapper.eq("PHONE", phone);
        }
        IPage<Consumptionlist> iPage = this.page(page, queryWrapper);
        return ResultBody.success(iPage.getRecords());
    }
}
